﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YouDaoNotesCodes.JSON.JArray和JObject封装JSON对象
{
    class JArryandJObject
    {
        //https://www.cnblogs.com/dacongge/p/6957074.html
        #region JObject 基本JSON对象
        public JObject GetJObject1()
        {
            var obj = new JObject { { "name", "zwq" } };
            return obj;
        }
        #endregion

        #region JObject 嵌套子对象（JObject中嵌套JObject）
        public JObject GetJObject2()
        {
            var obj1 = new JObject { { "id", "001" }, { "name", "zwq" } };
            var info1 = new JObject { { "age", "18" }, { "adress", "济南" } };
            obj1.Add("info1", info1);
            return obj1;
        }
        #endregion

        #region JArry 基本JSON对象中的数组
        public JArray GetJArry1()
        {
            var jarry = new JArray();
            var obj1 = new JObject { { "id", "001" }, { "name", "zwq" } };
            var obj2 = new JObject { { "age", "18" }, { "adress", "济南" } };
            jarry.Add(obj1);
            jarry.Add(obj2);
            return jarry;
        }
        #endregion

        #region JArry 多个JSON对象数组
        public JObject GetJArry2()
        {
            var obj = new JObject();
            var student = new JArray
        {
            new JObject {{ "Name", "Mark" }, { "Age", 8 } },
            new JObject {{ "Name", "Jack" }, { "Age", 9 } }
        };
            var results = new JArray
        {
            new JObject {{ "Subject", "语文"}, { "Score", 100}},
            new JObject {{ "Subject", "数学" }, { "Score", 88}}
        };
            obj.Add("Student", student);
            obj.Add("Results", results);
            return obj;
        }
        #endregion

        #region JArray：json数组嵌套数组（一个学生对应多个课程分数）
        public JObject GetJArry3()
        {
            var mark = new JObject { { "Name", "Mark" }, { "Age", "8" } };
            var results = new JArray
        {
            new JObject {{ "Subject", "语文"}, { "Score", 100}},
            new JObject {{ "Subject", "数学" }, { "Score", 88}}
        };
            mark.Add("Results", results);
            return mark;
        }
        #endregion
    }
}
